package dao

import (
	"context"
	"gorm.io/gorm"
	"listenboos-common/db"
	"listenboos-common/db/gorms"
	"ucenter/model"
)

type UserAccountDao struct {
	conn *gorms.GormConn
}

func NewUserAccountDao(db *db.ListenDB) *UserAccountDao {
	return &UserAccountDao{
		conn: gorms.New(db.Conn),
	}
}
func (u *UserAccountDao) FindUserAccount(ctx context.Context, userId int64) (account *model.UserAccount, err error) {
	session := u.conn.Session(ctx)
	err = session.Model(&model.UserAccount{}).Where("user_id = ?", userId).First(&account).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}
	return account, nil
}
func (u *UserAccountDao) CreateUserAccount(ctx context.Context, account *model.UserAccount) error {
	session := u.conn.Session(ctx)
	err := session.Create(&account).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return err
	}
	return nil
}
